<?php

namespace App\Http\Resources\CruiseCheck;

use App\Http\Resources\BaseResource;
use App\Models\Cruise\CruiseCheckDetailModel;
use App\Models\Cruise\CruiseCheckSelfDetailModel;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\MissingValue;
use Illuminate\Support\Collection;

class CruiseCheckSelfDetailListResource extends BaseResource
{
    public static ?Collection $rmsUserCollection = null;

    public static ?Collection $process = null;

    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        return [
            "id"                   => $this->id,
            "cruiseCheckCompanyId" => $this->cruiseCheckCompanyId,
            "taskId"               => $this->taskId,
            "date"                 => $this->date,
            "selfTaskResult"       => [
                "status"     => $this->status,
                "statusText" => translateEnumCodeToCn(CruiseCheckSelfDetailModel::STATUS_TEXT, $this->status),
            ],
            "fsCompamyName"        => $this->when(
                $this->resourceLoaded("cruiseCheckCompanyRel"),
                function () {
                    return object_get($this->cruiseCheckCompanyRel, "fsCompanyRel.name", "-");
                },
                new MissingValue()
            ),
            "cruiseOrderId"        => $this->cruiseOrderId,
            "process"              => $this->when(
                self::$process instanceof Collection,
                function () {
                    return attributeGet(
                        self::$process->where("orderId", $this->cruiseOrderId)->first(),
                        "process",
                        []
                    );
                },
                []
            ),
            "reportResult"         => [
                "id"               => object_get($this->cruiseCheckDetailsRel, "id", 0),
                "reportResult"     => object_get(
                    $this->cruiseCheckDetailsRel,
                    "reportResult",
                    CruiseCheckDetailModel::REPORT_RESULT_INIT
                ),
                "reportResultText" => translateEnumCodeToCn(
                    CruiseCheckDetailModel::REPORT_RESULT_MAPPING,
                    object_get($this->cruiseCheckDetailsRel, "reportResult", 0)
                ),
                "submitTime"       => formatDate(object_get($this->cruiseCheckDetailsRel, "submitTime", 0), "-"),
                "submitUid"        => object_get($this->cruiseCheckDetailsRel, "createdByUid", 0),
                "submitUsername"   => $this->when(
                    self::$rmsUserCollection instanceof Collection
                    &&
                    self::$rmsUserCollection->offsetExists(object_get($this->cruiseCheckDetailsRel, "createdByUid", 0)),
                    function () {
                        return self::$rmsUserCollection->offsetGet(object_get($this->cruiseCheckDetailsRel,
                            "createdByUid", 0))["username"];
                    },
                    "-"
                )
            ]
        ];
    }
}
